package com.dream21th.generate.util;

import com.dream21th.generate.dto.GenerateDTO;
import com.dream21th.generate.dto.MysqlDTO;
import com.dream21th.generate.enums.TemplateEnum;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.*;
import java.net.URL;
import java.util.*;

/**
 * @Auther: hp
 * @Date: 2019/6/11 22:41
 * @Description:
 */
public class FreeMakeUtils {

    public final static String CODE_PATH_TMP_CREATE="/home/app/tmp/create";
    public final static String CODE_PATH_TMP_ZIP="/home/app/tmp/zip";

    public static void generate(GenerateDTO dto) throws IOException, TemplateException {
        MysqlDTO mysql = dto.getMysql();
        dto.setTableName(mysql.getTableName());
        dto.setBeanName(TypeTranferUtils.tableNameToJava(dto.getTableName()));

        transferNullValue(dto);
        // 拼装数据
        freemarker.template.Configuration config = new freemarker.template.Configuration();
        config.setObjectWrapper(new DefaultObjectWrapper());

        ClassLoader classLoader = FreeMakeUtils.class.getClassLoader();

        URL resource = classLoader.getResource("template/entity.ftl");
        String path = resource.getPath();
        System.out.println(path);
        config.setDirectoryForTemplateLoading(
                new File(path.substring(0,path.lastIndexOf("/")+1)));

        Writer out=null;
        for(TemplateEnum template : TemplateEnum.values()){
            Template formBeanTemplate = config.getTemplate(template.getCode(), "UTF-8");
            Map<String, String> map = beanNameTranfer(template, dto);
            String outPutDir=CODE_PATH_TMP_CREATE+"/"+dto.getPackageName().replace(".","/")+"/"+map.get("createPath");
            File fileDir = new File(outPutDir);
            if (!fileDir.exists()) {
                fileDir.mkdirs();
            }
            String outPutFile=outPutDir+"/"+map.get("createName")+"."+template.getSuffix();
            out= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outPutFile), "UTF-8"));
            formBeanTemplate.process(dto, out);
            out.flush();
        }
        out.close();
    }

    private static void transferNullValue(GenerateDTO dto) {
        if(Objects.isNull(dto.getPackageName())){
            dto.setPackageName("com.dream21th");
        }
        if(Objects.isNull(dto.getEntityPath())){
            dto.setEntityPath("entity");
        }
        if(Objects.isNull(dto.getAuthor())){
            dto.setAuthor("unknown");
        }
        if(Objects.isNull(dto.getDtoPath())){
            dto.setDtoPath("dto");
        }
        if(Objects.isNull(dto.getMapperPath())){
            dto.setMapperPath("mapper");
        }
        if(Objects.isNull(dto.getServicePath())){
            dto.setServicePath("service");
        }
        if(Objects.isNull(dto.getControllerPath())){
            dto.setControllerPath("controller");
        }
        if(Objects.isNull(dto.getVuePath())){
            dto.setVuePath("vue");
        }

        if(Objects.isNull(dto.getVueAddPath())){
            dto.setVueAddPath("vue");
        }

        if(Objects.isNull(dto.getVueApiPath())){
            dto.setVueApiPath("vue");
        }
    }

    private static Map<String,String> beanNameTranfer(TemplateEnum template,GenerateDTO dto){
        Map<String,String> map=new HashMap<>();
        String createPath=null;
        String createName=null;
        switch (template){
            case ENTITY:
                createPath=dto.getEntityPath();
                createName=dto.getBeanName();
                break;
            case MAPPER:
                createPath=dto.getMapperPath();
                createName=dto.getBeanName()+"Mapper";
                break;
            case SERVICE:
                createPath=dto.getServicePath();
                createName="I"+dto.getBeanName()+"Service";
                break;
            case SERVICEIMPL:
                createPath=dto.getServicePath()+"/impl";
                createName=dto.getBeanName()+"ServiceImpl";
                break;
            case DTO:
                createPath=dto.getDtoPath();
                createName=dto.getBeanName()+"DTO";
                break;
            case MAPPERXML:
                createPath=dto.getMapperPath()+"/xml";
                createName=dto.getBeanName()+"Mapper";
                break;
            case CONTROLLER:
                createPath=dto.getControllerPath();
                createName=dto.getBeanName()+"Controller";
                break;
            case VUE:
                createPath=dto.getVuePath();
                createName=dto.getBeanName().substring(0,1).toLowerCase()+dto.getBeanName().substring(1);
                break;
            case VUEADD:
                createPath=dto.getVueAddPath();
                createName=dto.getBeanName().substring(0,1).toLowerCase()+dto.getBeanName().substring(1)+"-add-or-update";
                break;
            case VUEAPI:
                createPath=dto.getVueAddPath();
                createName=dto.getBeanName().substring(0,1).toLowerCase()+dto.getBeanName().substring(1);
                break;
            default:
                break;
        }
        map.put("createPath",createPath);
        map.put("createName",createName);
        return map;
    }
}
